package com.imooc.pan.server.common.utils;

import com.imooc.pan.core.constants.RPanConstants;

import java.util.Objects;

/**
 *
 */
public class UserIdUtil {
    private static final ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    public static void set(Long userId) {
        threadLocal.set(userId);
    }

    public static Long get() {
        Long userId = threadLocal.get();
        if (Objects.isNull(userId)) {
            return RPanConstants.ZERO_LONG;
        }
        return userId;
    }

    public static void remove() {
        threadLocal.remove();
        System.out.println(Thread.currentThread().getName() + "thread local value has been removed");
    }
}
